﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiniBottles.Models;
using MiniBottles.Repositories;
using System.IO;
using System.Web.UI;
using System.Text;

namespace MiniBottles.Controllers
{
    [Authorize]
    public class DictCategoryController : Controller
    {
        private DataContext db = new DataContext();
        private IDictCollectionRepository dictCollectionRepository;

        public DictCategoryController()
        {
            this.dictCollectionRepository = new DictCollectionRepository(db);
            
        }
        //
        // GET: /DictCategory/

        public ActionResult Index()
        {
            return View(db.DictCategory.Where(x => !x.IsDeleted).ToList());
        }

        ////
        //// GET: /DictCategory/Details/5

        //public ActionResult Details(int id = 0)
        //{
        //    DictCategory dictcategory = db.DictCategory.Find(id);
        //    if (dictcategory == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(dictcategory);
        //}

        //
        // GET: /DictCategory/Create

        public ActionResult Create(int collectionId = 0)
        {
            DictCategory category = new DictCategory();
            category.DictCollection = db.DictCollection.Find(collectionId);

            ViewBag.Collections = dictCollectionRepository.GetAll();
            return View(category);
        }

        //
        // POST: /DictCategory/Create

        [HttpPost]
        public ActionResult Create(DictCategory dictcategory, HttpPostedFileBase file)
        {

            ModelState.Remove("DictCollection.Name");
            
            if (ModelState.IsValid)
            {
                dictcategory.DictCollection = db.DictCollection.Find(dictcategory.DictCollection.Id);

                var path = Path.Combine(Server.MapPath("~/Upload/"));
                dictcategory.FileName = Helper.SaveFiles(file, path);

                db.DictCategory.Add(dictcategory);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Collections = dictCollectionRepository.GetAll();
            return View(dictcategory);
        }

        //
        // GET: /DictCategory/Edit/5

        public ActionResult Edit(int id = 0)
        {
            DictCategory dictcategory = db.DictCategory.Find(id);
            if (dictcategory == null)
            {
                return HttpNotFound();
            }
            ViewBag.Collections = dictCollectionRepository.GetAll();
            return View(dictcategory);
        }

        //
        // POST: /DictCategory/Edit/5

        [HttpPost]
        public ActionResult Edit(DictCategory dictcategory, HttpPostedFileBase file)
        {
            ModelState.Remove("DictCollection.Name");
            if (ModelState.IsValid)
            {
                DictCategory dictcategoryDb = db.DictCategory.Find(dictcategory.Id);

                dictcategoryDb.Name = dictcategory.Name;
                dictcategoryDb.IsActive = dictcategory.IsActive;
                dictcategoryDb.DictCollection = db.DictCollection.Find(dictcategory.DictCollection.Id);

                var path = Path.Combine(Server.MapPath("~/Upload/"));

                if (file != null)
                {
                    dictcategoryDb.FileName = Helper.SaveFiles(file, path);
                }
                

                db.Entry(dictcategoryDb).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Collections = dictCollectionRepository.GetAll();
            return View(dictcategory);
        }

        //
        // GET: /DictCategory/Delete/5

        public ActionResult Delete(int id = 0)
        {
            DictCategory dictcategory = db.DictCategory.Find(id);
            if (dictcategory == null)
            {
                return HttpNotFound();
            }
            ViewBag.Collections = dictCollectionRepository.GetAll();
            return View(dictcategory);
        }

        //
        // POST: /DictCategory/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            DictCategory dictcategory = db.DictCategory.Find(id);
            if (dictcategory.Bottles.Where(x => !x.IsDeleted).Count() < 1)
            {
                dictcategory.IsDeleted = true;
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult GetAllByCollection(int collectionId)
        {
            var list = db.DictCategory.Where(x => !x.IsDeleted && x.DictCollection.Id == collectionId).ToList();


            return Json(new { content = RenderPartialViewToString("_DropDownList", list) }); ;
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        protected string RenderPartialViewToString(string viewName, object model)
        {
            string toReturn;
            if (string.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString("action");

            ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);
                toReturn = sw.GetStringBuilder().ToString();
                //toReturn = RenderView(viewContext);
                return toReturn;

            }
        }
    }
}